package com.wbao.dianniu.ui;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lhh.apst.library.CustomPagerSlidingTabStrip2;
import com.lhh.apst.library.ViewHolder;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.tabstrip.fragments.APSTSViewPager;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 我的匿名
 * 
 * @author yejianjun
 *
 */
public class MyAmActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, OnClickListener {
	public CustomPagerSlidingTabStrip2 mAPSTS;
	public APSTSViewPager mVP;
	private SendAmFragment friendsFragment = null;
	private PartInAmFragment recomFragment = null;
	private static final int VIEW_FIRST = 0;
	private static final int VIEW_SECOND = 1;

	private static final int VIEW_SIZE = 2;
	private FragmentAdapter adapter;
	private Context mContext;
	private TextView titleTV;
	private ImageButton backButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Utils.setTranslucentStatus(this,true);
			SystemBarTintManager tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
			tintManager.setStatusBarTintResource(R.color.title_bg);// 通知栏所需颜色
		}
		setContentView(R.layout.activity_my_anonymous);
		mContext = this;
		titleTV = (TextView) findViewById(R.id.title);
		titleTV.setText(getResources().getString(R.string.anonymous_title));
		backButton = (ImageButton) findViewById(R.id.back_button);
		backButton.setOnClickListener(this);

		mAPSTS = (CustomPagerSlidingTabStrip2) findViewById(R.id.anonymous_tab);
		mVP = (APSTSViewPager) findViewById(R.id.anonymous_apsts);
		mVP.setOffscreenPageLimit(VIEW_SIZE);

		adapter = new FragmentAdapter(getSupportFragmentManager());
		mVP.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		mAPSTS.setIndicatorHeight(4);
		mAPSTS.setViewPager(mVP);
		mAPSTS.setOnPageChangeListener(this);
	}
	
	private class FragmentAdapter extends FragmentStatePagerAdapter implements
			CustomPagerSlidingTabStrip2.CustomTabProvider {
		protected LayoutInflater mInflater;

		public FragmentAdapter(FragmentManager fm) {
			super(fm);
			mInflater = LayoutInflater.from(mContext);
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			if (position >= 0 && position < VIEW_SIZE) {
				switch (position) {
				case VIEW_FIRST:
					friendsFragment = SendAmFragment.instance(mContext);
					return friendsFragment;

				case VIEW_SECOND:
					if (null == recomFragment)
						recomFragment = PartInAmFragment.instance(mContext);
					return recomFragment;
				default:
					break;
				}
			}

			return null;
		}

		@Override
		public int getCount() {
			return VIEW_SIZE;
		}

		@Override
		public View getSelectTabView(int position, View convertView) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.custom_select_tab_2,
						null);
			}
			TextView tv = ViewHolder.get(convertView, R.id.tvTab);
			tv.setText(getPageTitle(position));
			return convertView;
		}

		@Override
		public View getDisSelectTabView(int position, View convertView) {
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.custom_disselect_tab_2, null);
			}
			TextView tv = ViewHolder.get(convertView, R.id.tvTab);
			tv.setText(getPageTitle(position));
			return convertView;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case VIEW_FIRST:
				return getResources().getString(R.string.my_send_anonymous);
			case VIEW_SECOND:
				return getResources().getString(R.string.my_partin_anpnymous);
			default:
				break;
			}

			return super.getPageTitle(position);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub

	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
